home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / mint / editors / mntemacs.zoo / src / keyboard.c < prev    next >
C/C++ Source or Header  |  1992-04-28  |  58KB  |  2,272 lines

  1. /* Keyboard input; editor command loop.
  2.    Copyright (C) 1985, 1986, 1987, 1988, 1990 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /*** For version 19, can simplify this by making interrupt_input 1 on VMS.  */
  21.  
  22. /* Allow config.h to undefine symbols found here.  */
  23. #include <signal.h>
  24.  
  25. #include "config.h"
  26. #include <stdio.h>
  27. #undef NULL
  28. #include "termchar.h"
  29. #include "termopts.h"
  30. #include "termhooks.h"
  31. #include "lisp.h"
  32. #include "macros.h"
  33. #include "window.h"
  34. #include "commands.h"
  35. #include "buffer.h"
  36. #include <setjmp.h>
  37. #include <errno.h>
  38.  
  39. extern int errno;
  40.  
  41. /* Get FIONREAD, if it is available.  */
  42. #ifdef USG
  43. #include <termio.h>
  44. #include <fcntl.h>
  45. #else /* not USG */
  46. #ifndef VMS
  47. #include <sys/ioctl.h>
  48. #endif /* not VMS */
  49. #endif /* not USG */
  50.  
  51. /* Allow m- file to inhibit use of FIONREAD.  */
  52. #ifdef BROKEN_FIONREAD
  53. #undef FIONREAD
  54. #endif
  55.  
  56. /* Make all keyboard buffers much bigger when using X windows.  */
  57. #ifdef HAVE_X_WINDOWS
  58. #define BUFFER_SIZE_FACTOR 16
  59. #else
  60. #define BUFFER_SIZE_FACTOR 1
  61. #endif
  62.  
  63. /* Following definition copied from eval.c */
  64.  
  65. struct backtrace
  66.   {
  67.     struct backtrace *next;
  68.     Lisp_Object *function;
  69.     Lisp_Object *args;    /* Points to vector of args. */
  70.     int nargs;        /* length of vector */
  71.            /* if nargs is UNEVALLED, args points to slot holding list of unevalled args */
  72.     char evalargs;
  73.   };
  74.  
  75. /* Non-nil disable property on a command means
  76.  do not execute it; call disabled-command-hook's value instead. */
  77. Lisp_Object Qdisabled, Vdisabled_command_hook;
  78.  
  79. int recent_keys_index;    /* Index for storing next element into recent_keys */
  80. int total_keys;        /* Total number of elements stored into recent_keys */
  81. char recent_keys[100];    /* Holds last 100 keystrokes */
  82.  
  83. /* Buffer holding the key that invoked the current command.  */
  84. char *this_command_keys;
  85. int this_command_key_count;    /* Size in use.  */
  86. int this_command_keys_size;    /* Size allocated.  */
  87.  
  88. extern struct backtrace *backtrace_list;
  89.  
  90. static jmp_buf getcjmp;    /* for longjmp to where kbd input is being done. */
  91.  
  92. int waiting_for_input;    /* True while doing kbd input */
  93.  
  94. /* True while displaying for echoing.   Delays C-g throwing. */
  95. static int echoing;
  96.  
  97. int immediate_quit;    /* Nonzero means C-G should cause immediate error-signal. */
  98.  
  99. int help_char;        /* Character to recognize as the help char.  */
  100.  
  101. Lisp_Object Vhelp_form;    /* Form to execute when help char is typed.  */
  102.  
  103. /* Character that causes a quit.  Normally C-g.  */
  104.  
  105. int quit_char;
  106.  
  107. extern Lisp_Object global_map;
  108.  
  109. /* Current depth in recursive edits.  */
  110.  
  111. int command_loop_level;
  112.  
  113. /* Last input character read as a command.  */
  114.  
  115. int last_command_char;
  116.  
  117. /* Last input character read for any purpose.  */
  118.  
  119. int last_input_char;
  120.  
  121. /* If not -1, a character to be read as the next command input */
  122.  
  123. int unread_command_char;
  124.  
  125. /* Char to use as prefix when a meta character is typed in.
  126.  This is bound on entry to minibuffer in case Esc is changed there.  */
  127.  
  128. int meta_prefix_char;
  129.  
  130. /* Total number of times read_command_char has returned.  */
  131.  
  132. int num_input_chars;
  133.  
  134. /* Auto-save automatically when this many characters have been typed
  135.    since the last time.  */
  136.  
  137. static int auto_save_interval;
  138.  
  139. /* Value of num_input_chars as of last auto save.  */
  140.  
  141. int last_auto_save;
  142.  
  143. /* Last command executed by the editor command loop, not counting
  144.    commands that set the prefix argument.  */
  145.  
  146. Lisp_Object last_command;
  147.  
  148. /* The command being executed by the command loop.
  149.    Commands may set this, and the value set will be copied into last_command
  150.    instead of the actual command.  */
  151. Lisp_Object this_command;
  152.  
  153. Lisp_Object Qself_insert_command;
  154. Lisp_Object Qforward_char;
  155. Lisp_Object Qbackward_char;
  156.  
  157. /* read_key_sequence stores here the command definition of the
  158.    key sequence that it reads.  */
  159. Lisp_Object read_key_sequence_cmd;
  160.  
  161. /* Form to evaluate (if non-nil) when Emacs is started */
  162. Lisp_Object Vtop_level;
  163.  
  164. /* User-supplied string to translate input characters through */
  165. Lisp_Object Vkeyboard_translate_table;
  166.  
  167. FILE *dribble;            /* File in which we write all commands we read */
  168.  
  169. /* Nonzero if input is available */
  170. int input_pending;
  171.  
  172. /* Nonzero if should obey 0200 bit in input chars as "Meta" */
  173. int meta_key;
  174.  
  175. extern char *pending_malloc_warning;
  176.  
  177. /* Buffer for pre-read keyboard input */
  178. unsigned char kbd_buffer [256 * BUFFER_SIZE_FACTOR];
  179.  
  180. /* Number of characters available in kbd_buffer.  */
  181. int kbd_count;
  182.  
  183. /* Pointer to next available character in kbd_buffer.  */
  184. unsigned char *kbd_ptr;
  185.  
  186. /* Address (if not 0) of word to zero out
  187.  if a SIGIO interrupt happens */
  188. long *input_available_clear_word;
  189.  
  190. /* Nonzero means use SIGIO interrupts; zero means use CBREAK mode.
  191.    Default is 1 if INTERRUPT_INPUT is defined.  */
  192.  
  193. int interrupt_input;
  194.  
  195. /* Nonzero while interrupts are temporarily deferred during redisplay.  */
  196.  
  197. int interrupts_deferred;
  198.  
  199. /* nonzero means use ^S/^Q for flow control.  */
  200.  
  201. int flow_control;
  202.  
  203. #ifndef BSD4_1
  204. #define sigfree() sigsetmask (0)
  205. #define sigholdx(sig) sigsetmask (1 << ((sig) - 1))
  206. #define sigblockx(sig) sigblock (1 << ((sig) - 1))
  207. #define sigunblockx(sig) sigblock (0)
  208. #define sigpausex(sig) sigpause (0)
  209. #endif /* not BSD4_1 */
  210.  
  211. #ifdef BSD4_1
  212. #define SIGIO SIGTINT
  213. /* sigfree and sigholdx are in sysdep.c */
  214. #define sigblockx(sig) sighold (sig)
  215. #define sigunblockx(sig) sigrelse (sig)
  216. #define sigpausex(sig) sigpause (sig)
  217. #endif /* BSD4_1 */
  218.  
  219. #ifndef sigmask
  220. #define sigmask(no) (1L << ((no) - 1))
  221. #endif
  222.  
  223. /* We are unable to use interrupts if FIONREAD is not available,
  224.    so flush SIGIO so we won't try. */
  225. #ifndef FIONREAD
  226. #ifdef SIGIO
  227. #undef SIGIO
  228. #endif
  229. #endif
  230.  
  231. /* If we support X Windows, and won't get an interrupt when input
  232.    arrives from the server, poll periodically so we can detect C-g.  */
  233. #ifdef HAVE_X_WINDOWS
  234. #ifndef SIGIO
  235. #define POLL_FOR_INPUT
  236. #endif
  237. #endif
  238.  
  239. /* Function for init_keyboard to call with no args (if nonzero).  */
  240. void (*keyboard_init_hook) ();
  241.  
  242. static void read_avail_input ();
  243. static void get_input_pending ();
  244.  
  245. /* Non-zero tells input_available_signal to call read_socket_hook
  246.    even if FIONREAD returns zero.  */
  247. static int force_input;
  248.  
  249. static int echo_keystrokes;    /* > 0 if we are to echo keystrokes */
  250.  
  251. /* Nonzero means echo each character as typed.  */
  252. static int immediate_echo;
  253.  
  254. #define    min(a,b)    ((a)<(b)?(a):(b))
  255. #define    max(a,b)    ((a)>(b)?(a):(b))
  256.  
  257. static char echobuf[100];
  258. static char *echoptr;
  259.  
  260. /* Install the string STR as the beginning of the string of echoing,
  261.    so that it serves as a prompt for the next character.
  262.    Also start echoing.  */
  263.  
  264. echo_prompt (str)
  265.      char *str;
  266. {
  267.   int len = strlen (str);
  268.   if (len > sizeof echobuf - 4)
  269.     len = sizeof echobuf - 4;
  270.   bcopy (str, echobuf, len + 1);
  271.   echoptr = echobuf + len;
  272.  
  273.   echo ();
  274. }
  275.  
  276. /* Add the character C to the echo string,
  277.    if echoing is going on.  */
  278.  
  279. echo_char (c)
  280.      int c;
  281. {
  282.   extern char *push_key_description ();
  283.  
  284.   if (immediate_echo)
  285.     {
  286.       char *ptr = echoptr;
  287.  
  288.       if (ptr - echobuf > sizeof echobuf - 6)
  289.     return;
  290.  
  291.       ptr = push_key_description (c, ptr);
  292.       *ptr++ = ' ';
  293.       if (echoptr == echobuf && c == help_char)
  294.     {
  295.       strcpy (ptr, "(Type ? for further options) ");
  296.       ptr += strlen (ptr);
  297.     }
  298.  
  299.       *ptr = 0;
  300.       echoptr = ptr;
  301.  
  302.       echo ();
  303.     }
  304. }
  305.  
  306. /* Temporarily add a dash to the end of the echo string,
  307.    so that it serves as a mini-prompt for the very next character.  */
  308.  
  309. echo_dash ()
  310. {
  311.   if (!immediate_echo && echoptr == echobuf)
  312.     return;
  313.  
  314.   /* Put a dash at the end of the buffer temporarily,
  315.      but make it go away when the next charac